<?php

namespace backend\controllers;

use Yii;
use common\models\Fenhui\Fenhui;
use common\models\Fenhui\FenhuiSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use zhongkefu\zhongkefu;
use zhongkefu\helpers\Utils;
use common\models\adminlog\AdminLog;
use zhongkefu\helpers\Excelwriter;

/**
 * FenhuiController implements the CRUD actions for Fenhui model.
 */
class FenhuiController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Fenhui models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new FenhuiSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Fenhui model.
     * @param integer $id
     * @param integer $flag
     * @return mixed
     */
     public function actionView($id)
    {
        $model = $this->findModel($id);
        Adminlog::addlog(yii::$app->user->id, "查看会员 ".$model->contact);
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Creates a new Fenhui model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Fenhui();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id, 'flag' => $model->flag]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Fenhui model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @param integer $flag
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Fenhui model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @param integer $flag
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    public function actionUncheck(){
        $searchModel = new FenhuiSearch();
        $dataProvider = $searchModel->unchecksearch(Yii::$app->request->queryParams);

        return $this->render('uncheck', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    public function actionCheck(){
        $searchModel = new FenhuiSearch();
        $dataProvider = $searchModel->checksearch(Yii::$app->request->queryParams);

        return $this->render('check', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    public function actionWaitcheck(){
        $searchModel = new FenhuiSearch();
        $dataProvider = $searchModel->waitchecksearch(Yii::$app->request->queryParams);

        return $this->render('waitcheck', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Finds the Fenhui model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @param integer $flag
     * @return Fenhui the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
     protected function findModel($id)
    {
        if (($model = Fenhui::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /**
     * 审核按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStart()
    {

        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $id){
                $model = Fenhui::findOne(['id' => $id]);
                Fenhui::updateAll(['flag'=>Fenhui::STATUS_tongguo],['id' =>$model->id]);
            }
            return true;
        }
    }

    /**
     * 驳回按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStop()
    {

        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $reason = zhongkefu::$app->request->post()["reason"];
            $ids = explode(',',$ids);
            foreach($ids as $id){

                $model = Fenhui::findOne(['id' => $id]);
                Fenhui::updateAll(['flag'=>Fenhui::STATUS_DELETED,'reason'=>$reason],['id' =>$model->id]);
            }
            return true;
        }
    }

    
    function GetIpLookup($ip = ''){
        if(empty($ip)){
            return 'unkonw'; 
        }
        $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);  
        if(empty($res)){ return false; }
        $jsonMatches = array();
        preg_match('#\{.+?\}#', $res, $jsonMatches);
        if(!isset($jsonMatches[0])){ return false; }
        $json = json_decode($jsonMatches[0], true);
        if(isset($json['ret']) && $json['ret'] == 1){
            $json['ip'] = $ip;
            unset($json['ret']);
        }else{
            return false;  
        }
        return $json;  
    }
}
